package com.idea.zd.sys.train.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

public class TreadPool {


    /**
     * 非线程池
     * @param args
     * @throws Exception
     */
    /*public static void main(String [] args) throws Exception{
        Long start = System.currentTimeMillis();
        final Random random = new Random();
        final List<Integer> list = new ArrayList<>();


        for(int i=0;i<100000;i++){
            Thread thread = new Thread(){
                @Override
                public void run(){
                    list.add(random.nextInt());
                }
            };
            thread.start();
            thread.join();
        }

        System.out.println("用时："+(System.currentTimeMillis()-start));
        System.out.println("list长度"+list.size());

    }*/

    /**
     * 线程池
     * @param args
     * @throws Exception
     */
    public static void main(String [] args) throws Exception{
        Long start = System.currentTimeMillis();
        final Random random = new Random();
        final List<Integer> list = new ArrayList<>();

        ExecutorService executorService = Executors.newSingleThreadExecutor(); //初始化

        for(int i=0;i<100000;i++){

            Thread thread = new Thread(){
                @Override
                public void run(){
                    list.add(random.nextInt());
                    System.out.println(random.nextInt());
                }
            };
            Callable callable = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    return "seven";
                }
            };
             executorService.submit(thread);
             Future<String> f = executorService.submit(callable);
             System.out.println(f.get());
        }

        executorService.shutdownNow();
        System.out.println("用时："+(System.currentTimeMillis()-start));
        System.out.println("list长度"+list.size());

    }
}
